﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWCInvoice.Model;
using SWCInvoice.Enums;

namespace SWCInvoice.Returntyps {

    /// <summary>
    /// Result of the MergeDocuments method
    /// </summary>
    public class MergeDocumentsInfo : BaseInfo {

        #region Constanten

        /// Base object resource typ
        private const string RESOURCE_STRING = "MergeDocuments";

        #endregion

        #region Fields

        /// <summary>
        /// Documents with name and page count
        /// </summary>
        private List<KeyValuePair<string, int>> _documents;
        
        /// <summary>
        /// The processed page count
        /// </summary>
        private int _processedPageCount;

        #endregion

        #region Properties

        /// <summary>
        /// Base object resource typ
        /// </summary>        
        public override string ResourceString {
            get { return RESOURCE_STRING; }
        }

        /// <summary>
        /// Documents with name and page count
        /// </summary>
        public List<KeyValuePair<string, int>> Documents {
            get {
                if (_documents == null)
                    _documents = new List<KeyValuePair<string, int>>();
                return _documents; }
            set { _documents = value; }    
        }

        /// <summary>
        /// The processed page count
        /// </summary>
        public int ProcessedPageCount {
            get { return _processedPageCount; }
            set { _processedPageCount = value; }
        }

        #endregion
     
        #region Construcctor

        public MergeDocumentsInfo() {
            
        }

        #endregion

        #region Methods

        public override MessageTyp SetMessageTypByObjectContent() {
            if (Documents.Count > 0 && Documents.FindAll(item => item.Key.Equals(string.Empty)).Count > 0)
                MessageTyp = MessageTyp.Warning;
            else if (ProcessedPageCount == 0 && ExceptionList != null && ExceptionList.Count > 0)
                MessageTyp = MessageTyp.Error;
            else
                MessageTyp = MessageTyp.Information;

            return MessageTyp;
        }

        public override Message EnrichMessageWithParameters(Message message) {
            if (MessageTyp == MessageTyp.Warning || MessageTyp == MessageTyp.Information) {
                message.Description = string.Format(message.Description, Documents.Count);

                string tmpMessage = string.Empty;
                foreach (KeyValuePair<string, int> item in Documents) {
                    tmpMessage += string.Format("{0} • Name: {1}\t\t\t{2} Page(s)", "\r\n", (item.Key == string.Empty) ? "NA\t" : item.Key, item.Value);
                }
                message.Description = string.Concat(message.Description, "\r\n", tmpMessage);
            }

            if (MessageTyp == MessageTyp.Error)
                message.Description = string.Format(message.Description, ExceptionList[0].Message);


            return message;           
        }

        #endregion

    }
}
